<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/9
 * Time: 上午10:52
 */

namespace Api\Service;


class GoodsService extends BaseService
{
    protected $goodsModel;
    protected $goodsTypeModel;
    public function __construct()
    {
        $this->goodsTypeModel=M('goodstype');
        $this->goodsModel=M('goods');
    }

    /**
     * 根据goods_id查询出商品信息
     * @param $goods_id
     * @param null $filed
     * @return mixed
     */
    public function findGoodsInfoByGoodsId($goods_id,$filed=null){
        $where['goods_id']=$goods_id;
        if(empty($filed)){
           return $this->goodsModel->where($where)->find();
        }else{
           return $this->goodsModel->field($filed)->where($where)->find();
        }
    }

    /**
     * 根据条件查询出商品信息
     * @param $where
     * @param null $filed
     * @return mixed
     */
    public function findGoodsInfoByWhere($where,$filed=null){
        if(empty($filed)){
            $filed=self::goodsFiled();
            return $this->goodsModel->field($filed)->where($where)->find();
        }else{
            return $this->goodsModel->field($filed)->where($where)->find();
        }
    }

    //默认自查询字段
    public function goodsFiled(){
        return '
        goods_id,
        goods_name,
        shop_price,
        market_price,
        stock,is_new,
        is_hot,
        is_shiwu,
        stock,
        freight_payer,
        gtype_id,
        goods_pic,
        goods_thumb,
        goods_weight,
        goods_desc,
        grade_id,
        sale_all,
        shipping
        ';
    }

    /**,
     * 根据类型查询出商品信息
     * @param $type_id
     * @param null $filed
     * @return mixed
     */
    public function queryGoodsInfoByTypeId($type_id,$filed=null){
        $where['gtype_id']=$type_id;
        if(empty($filed)){
           return $this->goodsModel->where($where)->select();
        }else{
           return $this->goodsModel->field($filed)->where($where)->select();
        }
    }
    /**
     * 根据类型查询出商品信息
     * @param $where
     * @param null $filed
     * @return mixed
     */
    public function queryGoodsInfoByWhere($where,$filed=null){
        if(empty($filed)){
            return $this->goodsModel->where($where)->select();
        }else{
            return $this->goodsModel->field($filed)->where($where)->select();
        }
    }

    /**
     * 条件查询商品列表
     * @param $where
     * @param $page
     * @param int $page_num
     * @param null $filed
     * @param string $order_by
     * @return mixed
     */
    public function queryGoodsListByWhere($where,$page,$page_num=10,$filed=null,$order_by='ord ASC'){
        if(empty($filed)){
            return $this->goodsModel->where($where)->order($order_by)->page($page,$page_num)->select();
        }else{
            return $this->goodsModel->field($filed)->where($where)->order($order_by)->page($page,$page_num)->select();
        }
    }

    //返回商品类型
    public function backGoodsTypeInfo(){
        $goodsType=$this->goodsTypeModel->select();
        $back=array();
        foreach ($goodsType as $k=>$v){
            $back[$v['gtype_id']]=$v['gtype_name'];
        }
        return $back;
    }

    /**
     * 修改购物车数量
     * @param $uid
     * @param $goods_id
     * @param $goods_num
     * @return bool
     */
    public function saveCart($uid,$goods_id,$goods_num){
        $where['uid']=$uid;
        $where['goods_id']=$goods_id;
        $save['goods_num']=$goods_num;
        return $this->goodsModel->where($where)->save($save);
    }

}